package k.daniel.android.listview;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.lidroid.xutils.ViewUtils;

import java.util.List;

public abstract class BaseListAdapter<T> extends android.widget.BaseAdapter {
	public Context context;
	public List<T> mainDatas;

	public BaseListAdapter(Context context, List<T> datas) {
		super();
		this.context = context;
		this.mainDatas = datas;

	}

    public void delete(int position)
    {
        mainDatas.remove(position);
        notifyDataSetChanged();
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mainDatas.size();
	}

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    /**
	 * 建议使用getObject替代
	 */
	@Deprecated
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mainDatas.get(position);
	}
	public T getObject(int position) {
		// TODO Auto-generated method stub
		return mainDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public List<T> getMainDatas() {
		return mainDatas;
	}

	public void setMainDatas(List<T> mainDatas) {
		this.mainDatas = mainDatas;
		notifyDataSetChanged();

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = View.inflate(context, getLayoutId(), null);
			BaseViewHolder holder = getViewHolder(convertView);
			convertView.setTag(holder);
			convertView.setOnClickListener(holder);
		}
		BaseViewHolder holder = (BaseViewHolder) convertView.getTag();
		holder.position = position;
		onBindViewHolder(holder,position );
		return convertView;
	}

	/**
	 * 返回对应的Hodler.
	 * 
	 * @time 2015年3月5日
	 * @return
	 * @param convertView
	 */
	public abstract BaseViewHolder getViewHolder(View convertView);

	/**
	 * 处理每次GetView的数据
	 * 
	 * @time 2015年3月5日
	 * @param baseViewHolder
	 * @param position
	 */
	public abstract void onBindViewHolder(BaseViewHolder baseViewHolder, int position);

	/**
	 * 返回布局id
	 * 
	 * @time 2015年3月5日
	 * @return
	 */
	public abstract int getLayoutId();

	public class BaseViewHolder implements OnClickListener {
		public int position;
		public View convertView;

		public BaseViewHolder(View convertView) {
			this.convertView = convertView;
			ViewUtils.inject(this, convertView);
		}

		@Override
		/**
		 * 如果子类添加类事件,必须要调用super.onClick(v)
		 */
		public void onClick(View v) {
			// TODO Auto-generated method stub
			onItemClick(position, v);
		}
	}

	OnItemClickListener mOnItemClickListener;

	public OnItemClickListener getOnItemClickListener() {
		return mOnItemClickListener;
	}

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.mOnItemClickListener = onItemClickListener;
	}

	public void onItemClick(int position, View v) {
		if (mOnItemClickListener != null) {
			mOnItemClickListener.OnItemClick(this, position, v, getObject(position));
		}
	}

	public interface OnItemClickListener<T> {
		public void OnItemClick(BaseListAdapter<T> adapter, int position, View v, T t);
	}
}
